package com.ruoyi.app.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Console;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.app.api.domain.AppUser;
import com.ruoyi.app.domain.vo.AppUserCardVo;
import com.ruoyi.app.service.IAppNewUserService;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.satoken.utils.LoginHelper;
import com.ruoyi.system.api.domain.SysDictData;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.List;


/**
 * app页面接口
 * 前端访问路由地址为:/app/appuser
 *
 * @author Zach
 * @date 2023-11-14
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/appuser")
public class AppNewUserController extends BaseController {

    private final IAppNewUserService appNewUserService;

    /**
     * APP新用户注册
     * @param phone
     * @param password1
     * @param password2
     * @return
     */
    @PostMapping("registerAppNewUser")
    public  R<Object>  registerAppNewUser(@NotBlank(message = "手机号不能为空") String phone,
                                          @NotBlank(message = "密码不能为空") String password1,
                                          @NotBlank(message = "再次输入密码不能为空") String password2) {
        return appNewUserService.registerAppNewUser(phone, password1, password2);
    }

    /**
     * APP实名认证
     * @param userIdName
     * @param userIdCard
     * @param cardNo
     * @param cardBank
     * @param bankBranch
     * @param reservePhone
     * @param idCardFront
     * @param idCardBack
     * @return
     */
    @PostMapping("appNewUserAuth")
    public  R<Object>  appNewUserAuth(@NotBlank(message = "姓名不能为空") String userIdName, @NotBlank(message = "身份证号不能为空") String userIdCard,
                                      @NotBlank(message = "银行卡账号不能为空") String cardNo, @NotBlank(message = "请选择开户银行") String cardBank,
                                      @NotBlank(message = "开户支行不能为空") String bankBranch, @NotBlank(message = "预留手机不能为空") String reservePhone,
                                      @NotBlank(message = "请上传身份证正面") String idCardFront, @NotBlank(message = "请上传身份证背面") String idCardBack) {
        String userId = Convert.toStr(LoginHelper.getUserId());
        return appNewUserService.appNewUserAuth(userId, userIdName, userIdCard, cardNo, cardBank, bankBranch, reservePhone,idCardFront, idCardBack);
    }

    /**
     * 商户认证
     * @param name
     * @param priCategory
     * @param contactName
     * @param contactPhone
     * @param businessLicense
     * @param code
     * @return
     */
    @PostMapping("appMerchantAuth")
    public R<Object> appMerchantAuth(@NotBlank(message = "商家名称不能为空") String name, @NotBlank(message = "请选择主营类目") String priCategory,
                                      @NotBlank(message = "联系人姓名不能为空") String contactName, @NotBlank(message = "手机号码不能为空") String contactPhone,
                                      @NotBlank(message = "请上传营业执照") String businessLicense, @NotBlank(message = "验证码不能为空") String code) {
        String userId = Convert.toStr(LoginHelper.getUserId());
        return appNewUserService.appMerchantAuth(userId, name, priCategory, contactName, contactPhone, businessLicense, code);
    }

    /**
     * 添加信用卡
     * @param userIdName
     * @param userIdCard
     * @param cardNo
     * @param cardBank
     * @param phone
     * @param cardDate
     * @param cardEndorse
     * @return
     */
    @PostMapping("addCreditCard")
    public R<Object> addCreditCard(@NotBlank(message = "持卡人不能为空") String userIdName, @NotBlank(message = "身份证不能为空") String userIdCard,
                                     @NotBlank(message = "卡号不能为空") String cardNo, @NotBlank(message = "发卡行不能为空") String cardBank,
                                     @NotBlank(message = "手机号不能为空") String phone, @NotBlank(message = "有效期不能为空") String cardDate,
                                     @NotBlank(message = "CVN2不能为空") String cardEndorse) {
        String userId = Convert.toStr(LoginHelper.getUserId());
        return appNewUserService.addCreditCard(userId, userIdName, userIdCard, cardNo, cardBank, phone, cardDate, cardEndorse);
    }

    /**
     * 添加储蓄卡
     * @param userIdName
     * @param userIdCard
     * @param cardNo
     * @param cardBank
     * @param phone
     * @return
     */
    @PostMapping("addBankCard")
    public R<Object> addBankCard(@NotBlank(message = "持卡人不能为空") String userIdName, @NotBlank(message = "身份证不能为空") String userIdCard,
                                   @NotBlank(message = "卡号不能为空") String cardNo, @NotBlank(message = "发卡行不能为空") String cardBank,
                                   @NotBlank(message = "手机号不能为空") String phone) {
        String userId = Convert.toStr(LoginHelper.getUserId());
        return appNewUserService.addBankCard(userId, userIdName, userIdCard, cardNo, cardBank, phone);
    }

    /**
     * 获取字典数据列表 bank_list 银行列表 pri_category 主营类目
     * @param dictType
     * @return
     */
    @PostMapping("selectDictDataByType")
    public  R<List<SysDictData>> selectDictDataByType(@NotBlank(message = "dictType不能为空") String dictType) {
        return R.ok(appNewUserService.selectDictDataByType(dictType));
    }

    /**
     * 获取卡包列表 0 储蓄卡  1 信用卡
     * @param type
     * @return
     */
    @PostMapping("selectUserCard")
    public  R<List<AppUserCardVo>> selectUserCard(@NotBlank(message = "type不能为空") String type) {
        String userId = Convert.toStr(LoginHelper.getUserId());
        return R.ok(appNewUserService.selectUserCard(userId, type));
    }

    /**
     * 快捷收款
     * @param amount 收款金额
     * @param bankCard 到账储蓄卡
     * @param creditCard 收款信用卡
     * @param passway 收款通道
     * @return
     */
    @PostMapping("quickCollection")
    public  R<Object> quickCollection(@NotBlank(message = "收款金额不能为空") String amount,@NotBlank(message = "请选择到账储蓄卡") String bankCard,
                                      @NotBlank(message = "请选择收款信用卡") String creditCard,@NotBlank(message = "请选择收款通道") String passway) {
        Console.log("收款金额不能为空:{},到账储蓄卡:{},收款信用卡:{},收款通道:{}", amount, bankCard, creditCard, passway);
        return R.ok();
    }

    /**
     * 提现记录
     * @param pageNo
     * @param pageSize
     * @return
     */
    @PostMapping("cashOutList")
    public  R<List<JSONObject>> quickCollection(@NotBlank(message = "pageNo不能为空") String pageNo, @NotBlank(message = "pageSize不能为空") String pageSize) {
        List<JSONObject> resultList = new ArrayList();
        resultList.add(JSONObject.parseObject("{\"status\":\"提现成功\",\"amount\":\"-100.00元\",\"bankCard\":\"工商银行 储蓄卡 (2732)\",\"createTime\":\"2023-11-23\"}"));
        resultList.add(JSONObject.parseObject("{\"status\":\"收款成功\",\"amount\":\"+100.00元\",\"bankCard\":\"建设银行 信用卡 (2732)\",\"createTime\":\"2023-11-20\"}"));
        resultList.add(JSONObject.parseObject("{\"status\":\"提现成功\",\"amount\":\"-100.00元\",\"bankCard\":\"工商银行 储蓄卡 (2732)\",\"createTime\":\"2023-11-15\"}"));
        resultList.add(JSONObject.parseObject("{\"status\":\"收款成功\",\"amount\":\"+100.00元\",\"bankCard\":\"建设银行 信用卡 (2732)\",\"createTime\":\"2023-11-09\"}"));

        resultList.add(JSONObject.parseObject("{\"status\":\"提现成功\",\"amount\":\"-100.00元\",\"bankCard\":\"工商银行 储蓄卡 (2732)\",\"createTime\":\"2023-10-28\"}"));
        resultList.add(JSONObject.parseObject("{\"status\":\"收款成功\",\"amount\":\"+100.00元\",\"bankCard\":\"建设银行 信用卡 (2732)\",\"createTime\":\"2023-10-27\"}"));
        resultList.add(JSONObject.parseObject("{\"status\":\"收款成功\",\"amount\":\"+100.00元\",\"bankCard\":\"建设银行 信用卡 (2732)\",\"createTime\":\"2023-10-27\"}"));

        return R.ok(resultList);
    }
}
